﻿$(document).ready(function () {
        $('#zlight-nav').zlightMenu();
});
function UploadFile(files, callback) {

    var formFile = new FormData();

    for (var index in files) {
        formFile.append("file", files[index]);
    }

    $.ajax({
        url: "/Common/UploadFile",
        type: "POST",
        dataType: "JSON",
        data: formFile,
        processData: false,
        contentType: false,
        success: function (res) {
            if (!res.isSuccess) {
                AlertMessage("error", res.message)
                return;
            }
            callback && callback(res);
        },
        error: function () {
            alert("操作失败，请重试");
        }
    });
}

function AlertMessage(type, text) {

    if (type == "error") {
        $(".alert-msg").removeClass("alert-danger").removeClass("alert-success").addClass("alert-danger").text(text);
    }
    else if (type == "success") {
        $(".alert-msg").removeClass("alert-danger").removeClass("alert-success").addClass("alert-success").text(text);
    }

    $(".alert-msg").show();
}



/*图片大小缩放（start）*/
function ImageSizeUploadImage(el) {
    var $this = $(el);
    var files = $this.prop("files");

    var callback = function (res) {
        if (res.info.filePath.length == 1) {
            $("#qr-canvas").prop("src", res.info.filePath[0]);
            $("input[name='OldPath']").val(res.info.filePath[0]);
        }
    }
    UploadFile(files, callback);

}


function ImageSizeOnSuccess(res) {
    if (!res.isSuccess) {
        AlertMessage("error", res.message)
        $(".down-btn").prop("href", "javascript:;");
        return;
    }
    if (!!res.info.showUrl) {
        $(".show-img").prop("src", res.info.showUrl).prop("width", res.info.width).prop("height", res.info.height);
        $(".down-btn").prop("href", "/common/DownloadFile?filePath=" + res.info.showUrl + "");
    } else {
        $(".show-img").prop("src", "/images/img-0.png").prop("width", "120").prop("height", "120");
        $(".down-btn").prop("href", "javascript:;");
    }

}
/*图片大小缩放（end）*/

/*pdf转图片（start）*/
function PDFToImageUpload(el) {
    var $this = $(el);
    var files = $this.prop("files");

    var callback = function (res) {
        if (res.info.filePath.length == 1) {
            $("input[name='OldPath']").val(res.info.filePath[0]);
            $(".pdf-name").text(res.info.fileName[0]);
        }
    }

    UploadFile(files, callback);
}


function PDFToImageOnSuccess(res) {
    if (!res.isSuccess) {
        AlertMessage("error", res.message)
        $(".down-btn").prop("href", "javascript:;");
        return;
    }
    if (!!res.info.url) {
        $(".show-img").show();
        $(".pdf-image").text(res.info.msg);
        $(".down-btn").prop("href", "/common/DownloadFile?filePath=" + res.info.url + "").show();
    } else {
        $(".show-img").hide();
        $(".pdf-image").text("");
        $(".down-btn").prop("href", "javascript:;").hide();
    }

}
/*pdf转图片（end）*/


/*pdf加水印（start）*/
function PDFAddWatermarkUpload(el) {
    var $this = $(el);
    var files = $this.prop("files");

    var callback = function (res) {
        if (res.info.filePath.length == 1) {
            $("input[name='OldPath']").val(res.info.filePath[0]);
            $(".pdf-name").text(res.info.fileName[0]);
        }
    }

    UploadFile(files, callback);
}


function PDFAddWatermarkOnSuccess(res) {
    if (!res.isSuccess) {
        AlertMessage("error", res.message)
        $(".down-btn").prop("href", "javascript:;");
        return;
    }
    if (!!res.info.url) {
        $(".show-img").show();
        $(".pdf-image").text(res.info.msg);
        $(".down-btn").prop("href", "/common/DownloadFile?filePath=" + res.info.url + "").show();
    } else {
        $(".show-img").hide();
        $(".pdf-image").text("");
        $(".down-btn").prop("href", "javascript:;").hide();
    }

}
/*pdf加水印（end）*/


/*pdf合并（start）*/
function PDFMergeUpload(el) {
    var $this = $(el);
    var files = $this.prop("files");

    var callback = function (res) {
        if (res.info.filePath.length >= 1) {
            for (var i = 0; i < res.info.fileName.length; i++)
            {
                var file = '<p> ' + res.info.fileName[i] + '<img src="/images/delete.png" onclick="$(this).parent().remove()" style="width:14px;height:14px;margin-left:3px"/><input type="hidden" name="OldPath" value="' + res.info.filePath[i] +'"/></p>';
                $(".pdf-name").append(file);

            }
        }
    }

    UploadFile(files, callback);
}

function PDFMergeOnSuccess(res) {
    if (!res.isSuccess) {
        AlertMessage("error", res.message)
        $(".down-btn").prop("href", "javascript:;");
        return;
    }
    if (!!res.info.url) {
        $(".show-img").show();
        $(".pdf-image").text(res.info.msg);
        $(".down-btn").prop("href", "/common/DownloadFile?filePath=" + res.info.url + "").show();
    } else {
        $(".show-img").hide();
        $(".pdf-image").text("");
        $(".down-btn").prop("href", "javascript:;").hide();
    }

}
/*pdf合并（end）*/

/*通用上传文件（start）*/
function Upload(el, isMultiple = false) {
    var $this = $(el);
    var files = $this.prop("files");

    var callback = function (res) {
        for (var index in res.info.filePath) {
            var filePath = res.info.filePath[index];
            var fileName = res.info.fileName[index];
            var fileIcon = res.info.fileIcon[index];
            if (isMultiple) {
                var $img = '<p> <img src="' + fileIcon + '" class="upload-img-min"/>' + fileName + '<img src="/images/delete.png" onclick="$(this).parent().remove()" class="upload-file-delete"/><input type="hidden" name="pathList" value="' + filePath + '" /></p>';
                $this.closest(".row").next().find(".upload-list").append($img);
            } else {
                $this.parent().find(".file-path").val(filePath);
                $(".file-name").text(fileName);
                var $img = '<p> <img src="' + fileIcon + '" class="upload-img-min"/>' + fileName +'</p>';
                //var $img = $('<img src="' + fileIcon+'" class="upload-img" style="width: 100px; height: 100px;">');

                $this.closest(".row").next().find(".upload-list").html($img);
            }
        }
    }

    UploadFile(files, callback);
}


function OnSuccess(res) {
    if (!res.isSuccess) {
        //layer.alert("res.message");
        AlertMessage("error", res.message)
        $(".down-btn").prop("href", "javascript:;");
        return;
    }
    if (!!res.info.url) {
        $(".success-img").show();
        $(".success-msg").text(res.info.msg);
        $(".down-btn").prop("href", "/common/DownloadFile?filePath=" + res.info.url + "").show();
    } else {
        $(".success-img").hide();
        $(".success-msg").text("");
        $(".down-btn").prop("href", "javascript:;").hide();
    }

}
/*通用上传文件（end）*/

//切换选择
function SwitchType(el) {
    var $html = $(el);
    var type = $html.data("type");
    $("[data-type='" + type + "']").hide();
    $html.show();
}


/*视频加水印（start）*/
function VideoUpload(el) {
    var $this = $(el);
    var files = $this.closest("form").find("[type='file']").prop("files");

    var callback = function (res) {
        if (res.info.filePath.length == 1) {
            $("input[name='OldPath']").val(res.info.filePath[0]);
            $(".pdf-name").text(res.info.fileName[0]);
        }
    }

    UploadFile(files, callback);
}


function VideoOnSuccess(res) {
    if (!res.isSuccess) {
        AlertMessage("error", res.message)
        $(".down-btn").prop("href", "javascript:;");
        return;
    }
    if (!!res.info.url) {
        $(".show-img").show();
        $(".pdf-image").text(res.info.msg);
        $(".down-btn").prop("href", "/common/DownloadFile?filePath=" + res.info.url + "").show();
    } else {
        $(".show-img").hide();
        $(".pdf-image").text("");
        $(".down-btn").prop("href", "javascript:;").hide();
    }

}
/*视频加水印（end）*/